<?php


namespace app\command;


use app\common\Sms;
use app\services\utils\SmsService;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\facade\Db;

class CupboardAbnormalWebHook extends Command
{

    protected $type = [
        3 => '设备断电',
        8 => '设备断电',
        10 => '柜门异常',
        11 => '柜门异常',
        12 => '传感器脱落',
    ];

    protected $hook = [
        410100 => 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=89a36bc7-0ee4-4aa3-a0f3-9596c7b944af',
        610100 => 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=d30c0234-27ed-4114-aa70-14700bd3dfb5',
        510100 => 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=e9256a2f-469e-4fb1-8954-bb47a9c84aad',
        // 武汉
        420100 => 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=3167a1df-5bc7-4ec5-bb96-0a7115a6f624'
    ];

    protected function configure()
    {
        // 指令配置
        $this->setName('send_cupboard_abnormal_wx')->setDescription('货柜异常发送企业微信');
    }

    protected function execute(Input $input, Output $output)
    {
//        return $output->writeln('暂停执行');
        // 指令输出
        $maxId = Db::name('cupboard_abnormal_sms_record')->max('msg_id');
        if (!$maxId) {
            $time = '2022-10-12 18:30:00';
        }

        $where = [];
        $where[] = ['type', '=', 8];
        if ($maxId) {
            $where[] = ['id', '>', $maxId];
        }
        if (isset($time) && $time) {
            $where[] = ['created_at', '>=', $time];
        }
        $list = Db::connect('cupboard')->table('msg')->where($where)->field("id,type,cupboard_id")->select()->toArray();
//        $list = Db::connect('cupboard')->table('msg')->where('id',1875544)->field("id,type,cupboard_id")->select()->toArray();
        if (!$list) {
            $output->writeln('没有可执行的数据');
            return;
        }

        foreach ($list as $key => $value) {
            $cupboardInfo = Db::connect('cupboard')->table('cupboard')->where('id', $value['cupboard_id'])->find();
            if (!$cupboardInfo) {
                continue;
            }
            if (!$cupboardInfo['group_id']) {
                continue;
            }
            $city = $cupboardInfo['city'];
            if (!$city) {
                continue;
            }
            $cityId = Db::name('region')->where('name', $city)->value('id');
            if (!$cityId) {
                continue;
            }
            if (!isset($this->hook[$cityId])) {
                continue;
            }

            $franchiserInfo = Db::connect('cupboard')->table('franchiser')->where('id', $cupboardInfo['franchiser_id'])->find();
            if (!$franchiserInfo) {
                continue;
            }
            $bdmName = Db::name('admin_user')->where('id', $franchiserInfo['bdm_id'])->value("nick") ?: '未知';
            $bdName = Db::name('admin_user')->where('id', $franchiserInfo['bd_id'])->value("nick") ?: '未知';
            $franchiserName = $franchiserInfo['name'];
            $franchiserPhone = $franchiserInfo['phone'];
            $imei = $cupboardInfo['imei'];
            $problem = $this->type[$value['type']];

            $content = "【异常上报】BDM:$bdmName,BD:$bdName,您负责的商户$franchiserName($franchiserPhone),设备号$imei,出现$problem,请及时前往处理！";
            $params = [
                "msgtype" => "text",
                "text" => [
                    "content" => $content
                ]
            ];
            $result = curl_post($this->hook[$cityId], json_encode($params));
            $data = [
                'msg_id' => $value['id'],
                'send_phone' => $cityId,
                'content' => $content,
                'create_time' => date('Y-m-d H:i:s')
            ];
            Db::name('cupboard_abnormal_sms_record')->insert($data);
        }
        $output->writeln('货柜异常发送短信完成');
    }
}